﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Xmart.Action
{
    public class ActionResult
    {
        /// <summary>
        /// 消息
        /// </summary>
        public string Message { get; set; }
        /// <summary>
        /// 状态
        /// </summary>
        public int State { get; set; }

        /// <summary>
        /// 数据
        /// </summary>
        [JsonProperty(TypeNameHandling = TypeNameHandling.All)]
        public object Data { get; set; }

        /// <summary>
        /// 添加消息
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="args"></param>
        public void Append(string msg, params object[] args)
        {
            Message = string.Format(msg, args);
        }

        /// <summary>
        /// 是否成功
        /// </summary>
        public bool Success { get; set; }

        public override string ToString()
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(this);
        }

        public static ActionResult FromString(string json)
        {
            var ar = Newtonsoft.Json.JsonConvert.DeserializeObject<ActionResult>(json);

            return ar;
        }
    }
}
